import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import router from './router'
import Cookie from 'js-cookie'
import element from './components/elementui'
import store from '@/store/index'
//import JsonExcel from 'vue-json-excel'
//import Treetable from 'vue-table-with-tree-grid'
Vue.use(element)
Vue.use(VueRouter)
//Vue.component('treetable', Treetable)
//Vue.component('downloadExcel', JsonExcel)
Vue.prototype.$echarts = window.echarts

Vue.config.productionTip = false

router.beforeEach((to,from,next)=>{
  const token = Cookie.get('usertype')
  //当登陆页面没有获取到登陆被验证的token并且企图通过url的方式跳转时触发
  if(!token && to.name !== 'login'){
    console.log("请输入您的账号密码！")
    next({ name:'login' })
  }
  //当后台首页已登录但是企图通过url的方式跳转回登陆首页时触发
  else if(token && to.name == 'login'){
    console.log("请先退出才能重新登录")
    next({ name:'echarts' })
  }
  //正常加载
  else{
    next()
  }
})


new Vue({
  render: h => h(App),
  router,
  store,
  created(){
    store.commit('menu/enrollroute',router)
  }
}).$mount('#app')

